package com.example.experminent3;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);  // 简单布局

        // 进入页面后直接显示对话框
        showCustomDialog();
    }

    private void showCustomDialog() {
        try {
            View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_login, null);

            EditText etUsername = dialogView.findViewById(R.id.et_username);
            EditText etPassword = dialogView.findViewById(R.id.et_password);
            Button btnCancel = dialogView.findViewById(R.id.btn_cancel);
            Button btnSignIn = dialogView.findViewById(R.id.btn_sign_in);

            if (etUsername == null || etPassword == null || btnCancel == null || btnSignIn == null) {
                throw new RuntimeException("View not found in dialog_login.xml");
            }

            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setView(dialogView)
                    .create();

            btnCancel.setOnClickListener(v -> dialog.dismiss());

            btnSignIn.setOnClickListener(v -> {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                Toast.makeText(DialogActivity.this,
                        "用户名：" + username + "，密码：" + password,
                        Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            });

            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "对话框显示失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}